//
//  FRAdvertiseView.m
//  Live
//
//  Created by 徐芙蓉 on 2017/8/23.
//  Copyright © 2017年 Afra. All rights reserved.
//

#import "FRAdvertiseView.h"
#import "FRLiveHandler.h"
#import "FRAdvertise.h"
#import "FRCacheHelper.h"

static NSInteger showTime = 3;

@interface FRAdvertiseView ()
@property (weak, nonatomic) IBOutlet UIImageView *backView;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation FRAdvertiseView

+(instancetype)loadAdvertiseView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"FRAdvertiseView" owner:self options:nil] lastObject];
}

//广告页初始化方法
-(void)awakeFromNib
{
    [super awakeFromNib];
    self.frame = [UIScreen mainScreen].bounds;
    
    //展示广告
    [self showID];
    //下载广告
    [self downID];
    //倒计时
    [self startTimer];
}

-(void)showID
{
    NSString *filename = [FRCacheHelper getAdvertise];
    NSString *filePath = [NSString stringWithFormat:@"%@%@",IMAGE_HOST,filename];
   UIImage *lastCacheImage = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:filePath];
    if (lastCacheImage) {
        self.backView.image = lastCacheImage;
    } else {
        self.hidden = YES;
    }
}

-(void)downID
{
    //获取广告页数据
    [FRLiveHandler executeGetAdvertiseTaskWithSuccess:^(id obj) {
        FRAdvertise *ad = obj;
        NSURL *imageUrl = [NSURL URLWithString:ad.image];
        
        //SDWebImageAvoidAutoSetImage 下载完不给Imageview赋值
        [[SDWebImageManager sharedManager] loadImageWithURL:imageUrl options:SDWebImageAvoidAutoSetImage progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            
            NSLog(@"图片下载成功");
            
//            [[NSUserDefaults standardUserDefaults] setObject:ad.image forKey:@"adImage"];
//            [[NSUserDefaults standardUserDefaults] synchronize];
            [FRCacheHelper setAdvertise:ad.image];
            
        }];
        
        
    } failed:^(id obj) {
        NSLog(@"%@",obj);
    }];
}

-(void)startTimer
{
    __block NSUInteger timeout = showTime + 1;
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    self.timer = timer;
    dispatch_source_set_event_handler(timer, ^{
        if (timeout <=0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dissmiss];
            });
            
        } else {
           
            dispatch_async(dispatch_get_main_queue(), ^{
                self.timeLabel.text = [NSString stringWithFormat:@"跳过%zd",timeout];
            });
             timeout--;
        }
    });
    dispatch_resume(timer);
}

-(void)dissmiss
{
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.f;
        [self removeFromSuperview];
    }];
}

@end
